package njClass.base{
	import flash.events.Event;
	import flash.text.TextField;
	
	public class ObjectPlus{
		private static var Oname:String;
		private static var sorts:String;
		
		//Object数组(array:Array)特定参数(name:String)排序
		public static function SortObjectArray(array:Array,name:String,sort:String="desc"):Array{
			Oname=name;
			sorts=sort;
			return array.sort(OneByOne);
		}
		
		private static function OneByOne(ObjectA:Object,ObjectB:Object):Number{
			if(int(ObjectA[Oname])>int(ObjectB[Oname])){
				if(sorts=="desc"){
					return -1;
				}else{
					return 1;
				}
			}else if(int(ObjectA[Oname])<int(ObjectB[Oname])){
				if(sorts=="desc"){
					return 1;
				}else{
					return -1;
				}
			}else{
				return 0;
			}
		}
		
		//输出Object对象内容
		public static function checkAndOutput(obj:*,id:int=0,ValueNames:Array=null,SimpleMode:Boolean=false):String{
			var message:String="";
			if(typeof(obj)=="object"){
				var oneNames:Array=new Array();
				if (ValueNames == null){
					for(var k:*in obj){
						oneNames.push(String(k));
					}
				}else{
					oneNames=ValueNames;
				}
				if(!SimpleMode){
					message+=("[" + id + "]:" + obj + "\n");
				}
				for(var j:int=0; j < oneNames.length; j++){
					if(!SimpleMode){
						message+=(oneNames[j] + " : " + obj[oneNames[j]] + "\n");
					}else{
						message+=oneNames[j];
						message+=obj[oneNames[j]];
					}
				}
				oneNames=null;
			}else{
				if(!SimpleMode){
					message+=("[" + id + "]:" + typeof(obj) + "\n");
					message+=(obj + "\n");
				}else{
					message+=obj;
				}
			}
			return message;
		}
		
		//把xml转换为以Object为主要元素的array，其中字节名为name,字节为value,属性以属性名命名，格式根据xml排列
		public static function xmlToArray(xml:XML):Array{
			var array:Array=new Array();
			if(xml.children().length()>1){
				array=checkLevels(xml);
			}else{
				array.push(checkAndInsert(xml));
			}
			return array;
		}
		
		private static function checkAndInsert(xml:XML,levels:Boolean=false):Object{
			var obj:Object=new Object();
			obj={name:xml.name()};
			if(xml!=""){
				obj["value"]=xml;
			}
			for(var j:int=0;j<xml.attributes().length();j++){
				obj[String(xml.attributes()[j].name())]=xml.attributes()[j];
			}
			return obj;
		}
		
		private static function checkLevels(xml:XML):Array{
			var array:Array=new Array();
			for(var i:int=0;i<xml.children().length();i++){
				if(xml.children()[i].children().length()>1){
					array[i]=checkLevels(xml.children()[i]);
				}else{
					array[i]=checkAndInsert(xml.children()[i]);
				}
			}
			return array;
		}
		
		//比较任意两元素
		public static function compareObject(obj1:Object,obj2:Object):Boolean{
			if(checkAndOutput(obj1,0,null,true)==checkAndOutput(obj2,0,null,true)){
				return true;
			}else{
				return false;
			}
		}
		
		//variable为function():*的函数，要求返回监听参数，object为绑定物，property为绑定物上的属性
		public static function bindable(variable:Function,object:*,property:String):void{
			//trace(myAs.nastr);
			var txt:TextField=new TextField();
			txt.text=variable().toString();
			
			txt.addEventListener(Event.ENTER_FRAME,checkChange);
			
			function checkChange(event:Event):void{
				if(txt.text!=typeof(variable()) + variable().toString()){
					txt.text=typeof(variable()) + variable().toString();
					changeObject();
				}
			}
			
			function changeObject():void{
				object[property]=variable();
			}
		}
	}
}